package projcet.test.student.student;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class QueryActivity extends AppCompatActivity {

    @BindView(R.id.edit_query_no)
    EditText editQueryNo;
    @BindView(R.id.bt_select)
    Button btSelect;
    @BindView(R.id.edit_query_name)
    EditText editQueryName;
    @BindView(R.id.edit_student_no)
    EditText editStudentNo;
    @BindView(R.id.edit_student_class)
    EditText editStudentClass;
    @BindView(R.id.lv_show)
    ListView lvShow;

    private List<StudentInfo>       mData = new ArrayList<StudentInfo>();;
    private List<Info>         mInfo = new ArrayList<Info>();;

    private DbOpenHelper mHelper;
    String  studentNo;

    QueryAdapter queryAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query);
        ButterKnife.bind(this);
        mHelper = new DbOpenHelper(this);
    lvShow.setDividerHeight(10);
        getStudentInfoList();
    }

    /**
     * 查出结果并展示
     */
    @OnClick(R.id.bt_select)
    public void onViewClicked() {
        studentNo=editQueryNo.getText().toString();
        if(TextUtils.isEmpty(studentNo)){
            Toast.makeText(this, "请输入学号", Toast.LENGTH_SHORT).show();
        }else {
            getStudentInfoListByNo(studentNo);
            editStudentClass.setText(mData.get(0).getClassName());
            editQueryName.setText(mData.get(0).getName());
            editStudentNo.setText(mData.get(0).getStudentNo());
            LayoutInflater inflater = getLayoutInflater();
            queryAdapter =new QueryAdapter(mData,this,inflater,mInfo);
            lvShow.setAdapter(queryAdapter);
          queryAdapter.notifyDataSetChanged();
        }
    }


    /**
     * 根据号查数据库
     */
    private List<StudentInfo> getStudentInfoListByNo(String no) {

        SQLiteDatabase sqLiteDatabase = mHelper.getWritableDatabase();
        String  where="studentNo=?";
        String  [] arg= {no};
        Cursor cursor = sqLiteDatabase.query("Student", null, where, arg, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String studentNo = cursor.getString(cursor.getColumnIndex("studentNo"));
                String className = cursor.getString(cursor.getColumnIndex("className"));
                String subject = cursor.getString(cursor.getColumnIndex("subject"));
                int  score = cursor.getInt(cursor.getColumnIndex("score"));

                mData.add(new StudentInfo(name, studentNo, className,subject ,score));
            } while (cursor.moveToNext());
        }
        cursor.close();

        return mData;
    }



    /**
     * 获得数据库中学生的数据
     */
    private List<Info> getStudentInfoList() {

        SQLiteDatabase sqLiteDatabase = mHelper.getWritableDatabase();
        Cursor cursor = sqLiteDatabase.query("Teacher", null, null, null, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                String teacher = cursor.getString(cursor.getColumnIndex("teacher"));
                String subject = cursor.getString(cursor.getColumnIndex("subject"));


                mInfo.add(new Info(teacher, subject));
            } while (cursor.moveToNext());
        }
        cursor.close();

        return mInfo;
    }

}
